<?php
abstract class Util {

	static function getURLTitleFor($inputString) {
		$inputString = trim($inputString);
		$outputString = strtolower(utf8_decode($inputString));
		$outputString = preg_replace('/\&/', ' and ', $outputString);
		$outputString = preg_replace('/\//', ' ', $outputString);
		$outputString = preg_replace('/[\xC0-\xC5\xE0-\xE5]/', 'a', $outputString);
		$outputString = preg_replace('/[\xE6]/', 'ae', $outputString);
		$outputString = preg_replace('/[\xC7\xE7]/', 'c',$outputString);
		$outputString = preg_replace('/[\x82\xC8-\xCB\xE8-\xEB]/', 'e',$outputString);
		$outputString = preg_replace('/[\xCC-\xCF\xEC-\xEF]/', 'i',$outputString);
		$outputString = preg_replace('/[\xF1\xD1]/', 'n',$outputString);
		$outputString = preg_replace('/[\xF2-\xF6\xF8\xD8\xD2-\xD6]/', 'o',$outputString);
		$outputString = preg_replace('/[\x8A\x9A]/', 's',$outputString);
		$outputString = preg_replace('/[\xD9\xDA\xDB\xDC\xF9-\xFC]/', 'u',$outputString);
		$outputString = preg_replace('/[\xFD\xFF\x9F\xDD]/', 'y',$outputString);
		$outputString = preg_replace('/[\x9E]/', 'z',$outputString);
		$outputString = preg_replace('/[^[[:alnum:]\-\s]/', '',$outputString);
		$outputString = preg_replace('/\s{1,}/', '-',$outputString);
		$outputString = preg_replace('/-{1,}/', '-',$outputString);
		$outputString = preg_replace('/-$/', '',$outputString);
		return $outputString;
	}

	static public function createUniqueHash($extra_salt=null){
		return sha1(uniqid('lypo').microtime().md5(rand()).md5($extra_salt));
	}
	
	static public function slugify($text)
	{
		// replace non letter or digits by -
		$text = preg_replace('~[^\\pL\d]+~u', '-', $text);

		// trim
		$text = trim($text, '-');

		// transliterate
		if (function_exists('iconv'))
		{
			$text = iconv('utf-8', 'us-ascii//TRANSLIT', $text);
		}

		// lowercase
		$text = strtolower($text);

		// remove unwanted characters
		$text = preg_replace('~[^-\w]+~', '', $text);

		if (empty($text))
		{
			return 'n-a';
		}

		return $text;
	}



	static function searchResultTrim($value,$numwords=100){
		$words = explode(' ',strip_tags($value));
		if (count($words) > $numwords){
			array_splice($words, $numwords);
			$value = implode(' ', $words);
			$value .= " ... ";
		}
		return $value;
	}

	static function searchResultTrimChars($value,$numchars=100, $postfix=''){
		$numchars = $numchars - strlen($postfix);
		if (strlen($value) > $numchars){
			$value = substr($value,0,$numchars);
			$value .= " ... ";
		}
		return $value;
	}

	public static function HumanReadableFilesize($size) {

		// Adapted from: http://www.php.net/manual/en/function.filesize.php

		$mod = 1024;

		$units = explode(' ','B KB MB GB TB PB');
		for ($i = 0; $size > $mod; $i++) {
			$size /= $mod;
		}

		return round($size, 2) . ' ' . $units[$i];
	}

	public static function switchToApplication($options=array()){
		
		$currentOptions = array(
			'app' => sfConfig::get('sf_app'),
			'env' => sfConfig::get('sf_environment'),
			'debug' => sfConfig::get('sf_debug'), 
			'no_script_name' => sfConfig::get('sf_no_script_name'),
		);
		
		$options = array_merge($currentOptions,$options);
		
		$configuration = ProjectConfiguration::getApplicationConfiguration($options['app'],$options['env'],$options['debug']);
		sfContext::createInstance($configuration);
		sfConfig::set('script_name',$options['no_script_name']);
		return $currentOptions;
	}
}
?>